package com.ugoodtech.mdcc.client.controller;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.dto.BrandVO;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.dto.LabelValue;
import com.ugoodtech.mdcc.client.dto.ServerResponse;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.repository.BrandRepository;
import com.ugoodtech.mdcc.core.repository.QuestionAnswerOptionRepository;
import com.ugoodtech.mdcc.core.repository.UserProfileRepository;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

@Api(description = "获取品牌接口")
@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private BrandRepository brandRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;


    @ApiOperation("获取当前自己负责的品牌")
    @RequestMapping(value = "/getCreateBrand", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getCreateBrand(Principal principal,
                                   @ApiParam("品牌关键字") @RequestParam(value = "brandname", required = false) String brandName) {
        if (principal == null) {
            return JsonResponse.errorResponseWithError("抱歉", "invalid_token");
        }
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        UserProfile userProfile = userProfileRepository.findByUserId(clientUser.getId());
        if (userProfile == null) {
            return JsonResponse.errorResponseWithError("抱歉", "当前登陆用户信息不完整");
        }
        if (clientUser.getUserType().equals(UserType.dealers)) {
        return getAllBrand();
        }
        List<Brand> brandList = userProfile.getBrandList();
        String[] brandArray = new String[brandList.size()];
        for (int i = 0; i < brandList.size(); i++) {
            brandArray[i] = brandList.get(i).getBrandName();
        }
        return JsonResponse.successResponseWithData(brandArray);
    }

    @ApiOperation("获取当前自己负责的品牌")
    @RequestMapping(value = "/getMyBrand", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getMyBrand(Principal principal,
                                   @ApiParam("品牌关键字") @RequestParam(value = "brandname", required = false) String brandName) {
        if (principal == null) {
            return JsonResponse.errorResponseWithError("抱歉", "invalid_token");
        }
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        UserProfile userProfile = userProfileRepository.findByUserId(clientUser.getId());
        if (userProfile == null) {
            return JsonResponse.errorResponseWithError("抱歉", "当前登陆用户信息不完整");
        }
//        if (clientUser.getUserType().equals(UserType.dealers)) {
        return getAllBrand();
//        }
//        List<Brand> brandList = userProfile.getBrandList();
//        String[] brandArray = new String[brandList.size()];
//        for (int i = 0; i < brandList.size(); i++) {
//            brandArray[i] = brandList.get(i).getBrandName();
//        }
//        return JsonResponse.successResponseWithData(brandArray);
    }

    @ApiOperation("获取全部品牌")
    @RequestMapping(value = "/getAllBrand", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getAllBrand() {
        List<Brand> brandList = (List<Brand>) brandRepository.findAll();
        String[] brandArray = new String[brandList.size()];
        for (int i = 0; i < brandList.size(); i++) {
            brandArray[i] = brandList.get(i).getBrandName();
        }
        return JsonResponse.successResponseWithData(brandArray);
    }

    @ApiOperation("获取全部品牌")
    @RequestMapping(value = "/getAllAnswerBrand", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getAllAnswerBrand(@ApiParam("品牌关键字") @RequestParam(value = "brandname", required = false) String brandname) {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(8L));
        if (StringUtils.isNotEmpty(brandname)) {
            builder.and(qQuestionAnswerOption.name.like("%" + brandname + "%"));
        }
        Page<QuestionAnswerOption> answerOptionList = questionAnswerOptionRepository.findAll(builder, new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        List<LabelValue> labelValues = new ArrayList<>();
        for (QuestionAnswerOption answerOption : answerOptionList.getContent()) {
            LabelValue labelValue = new LabelValue(answerOption.getName(), answerOption.getId() + "");
            labelValues.add(labelValue);
        }
        return JsonResponse.successResponseWithData(labelValues);
    }

    @ApiOperation("获取投诉报告的品牌")
    @RequestMapping(value = "/getComplaintBrand", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getComplaintBrand(@ApiParam("品牌关键字") @RequestParam(value = "brandname", required = false) String brandname) {
        QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qQuestionAnswerOption.deleted.eq(false));
        builder.and(qQuestionAnswerOption.enabled.eq(true));
        builder.and(qQuestionAnswerOption.question.id.eq(194L));
        if (StringUtils.isNotEmpty(brandname)) {
            builder.and(qQuestionAnswerOption.name.like("%" + brandname + "%"));
        }
        Page<QuestionAnswerOption> answerOptionList = questionAnswerOptionRepository.findAll(builder, new PageRequest(0, 1000, new Sort(Sort.Direction.ASC, "idx")));
        List<LabelValue> labelValues = new ArrayList<>();
        for (QuestionAnswerOption answerOption : answerOptionList.getContent()) {
            LabelValue labelValue = new LabelValue(answerOption.getName(), answerOption.getId() + "");
            labelValues.add(labelValue);
        }
        return JsonResponse.successResponseWithData(labelValues);
    }
    @RequestMapping(value = "/brands", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse listAllBrands(@RequestParam(required = false, defaultValue = "")String brand) {
        return ServerResponse.success(ServerResponse.MSG_SUCCESS, listAllBrandsVO(brand));
    }
    List<BrandVO> listAllBrandsVO(String brand){
        QBrand qBrand =QBrand.brand;
        BooleanBuilder booleanBuilder =new BooleanBuilder();
        if (StringUtils.isNotEmpty(brand)) {
            booleanBuilder.and(qBrand.brandName.like("%" + brand + "%"));
        }
        List<BrandVO> brandVOS=new ArrayList<>();
        Iterable<Brand> all = brandRepository.findAll(booleanBuilder);
        for (Brand brand1 : all) {
            BrandVO brandVO =new BrandVO();
            brandVO.setBrandId(brand1.getId());
            brandVO.setBrandName(brand1.getBrandName());
            brandVO.setId(brand1.getId());
            brandVOS.add(brandVO);
        }
        return brandVOS;
    }
}
